package com.example.demo;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.bean.Person;
import com.example.mapper.PersonMapper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class UserGetController {
	@Autowired
	private PersonMapper personMapper;
	
	@RequestMapping(value = "/getPerson")
	public String getUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String page=request.getParameter("page");
		String rows=request.getParameter("rows");
		List<Person>listTotal=personMapper.findAllPerson();
		List<Person>list=personMapper.findAll((Integer.parseInt(page)-1)*Integer.parseInt(rows),rows);
		Map<String,Object>map=new HashMap<String, Object>();
		map.put("total",listTotal.size());
		map.put("rows",list);
		//JSONArray json=JSONArray.fromObject(list);
		JSONObject json=JSONObject.fromObject(map);
		PrintWriter out=response.getWriter();
		out.write(json.toString());
		return null;
	}

	@RequestMapping(value = "/initPerson")
	public String initPerson(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		return "jsp/crudTest";
	}
	@RequestMapping(value = "/DoPersonService")
	public String DoPersonService(HttpServletRequest request,
			HttpServletResponse response,@RequestParam (value="operate") String f,Person person) throws Exception{
		if(null!=f&&f.length()>0){
			if("add".equals(f)){//新增
				personMapper.save(person);
			}else if("update".equals(f)){//更新
				personMapper.update(person);
			}else if("delete".equals(f)){//删除
				personMapper.delete(person.getId());
			}
		}
		PrintWriter out=response.getWriter();
		out.write("{'success':'1','message':'成功'}");
				return null;
		
	}
	@RequestMapping(value = "/initTree")
	public String initTree(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		return  "jsp/tree";
	}
	
	@RequestMapping(value = "/getTree")	
	public String getTree(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		PrintWriter out=response.getWriter();
		//out.print("[{\"id\":\"1\",\"text\":\"S11\",\"state\":\"open\",\"children\":[{\"id\":\"11\",\"text\":\"Node 11\"},{\"id\":\"v12\",\"text\":\"Node 12\"}]},{\"id\":\"2\",\"text\":\"Node 2\",\"state\":\"closed\"}]");
		Map<String,Object>map=new HashMap<String, Object>();
		List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
		
		Map<String,Object>map1=new HashMap<String, Object>();
		map1.put("id","11");
		map1.put("text","Node 11");
		Map<String,Object>map2=new HashMap<String, Object>();
		map2.put("id","12");
		map2.put("text","Node 12");
		list.add(map1);
		list.add(map2);
		
		map.put("id","1");
		map.put("text","S11");
		map.put("state","OPEN");
		map.put("children",list);
		Map<String,Object>mapOther=new HashMap<String, Object>();
		mapOther.put("id","2");
		mapOther.put("text","Node 21");
		mapOther.put("state","closed");
		List<Map<String,Object>>listresult=new ArrayList<Map<String,Object>>();
		listresult.add(map);
		listresult.add(mapOther);
		JSONArray json=JSONArray.fromObject(listresult);
		out.print(json.toString());
		return null;
	}
}
